home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / StandardFileIcons / SFIconsInit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-23  |  3.9 KB  |  149 lines  |  [TEXT/MPS ]

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    StandardFileIcons: A sample control panel changing behavior of StandardFile.
  5. **
  6. **    by Brian Bechtel, Apple Developer Technical Support
  7. **
  8. **    File:        SFIconsInit.c
  9. **
  10. **    Copyright © 1994 Apple Computer, Inc.
  11. **    All rights reserved.
  12. **
  13. **    You may incorporate this sample code into your applications without
  14. **    restriction, though the sample code has been provided "AS IS" and the
  15. **    responsibility for its operation is 100% yours.  However, what you are
  16. **    not permitted to do is to redistribute the source as "DTS Sample Code"
  17. **    after having made changes. If you're going to re-distribute the source,
  18. **    we require that you make it clear in the source that the code was
  19. **    descended from Apple Sample Code, but that you've made changes.
  20. **
  21. ** Change history (most recent first)
  22. **
  23. **
  24. **  <2> 940923  BL°B    Jon Pugh noticed that I was calling DisposeHandle
  25. **                        on a resource.  This, as they say, is evil.
  26. **    <1>    940811    BL°B    Check for System 7.  Display failure icon and do
  27. **                        nothing else if earlier than System 7.  Macintosh
  28. **                        Easy Open is the first product which actually
  29. **                        provided the desktop icon feature; it can work all
  30. **                        the way back to System 7, potentially.
  31. **    <0>    940802    BL°B    First implementation
  32. */
  33.  
  34. /*
  35. ** Check for the existence of our SFIcons preference file.  If the file exists,
  36. ** then we want to use generic icons instead of the color icons Standard File Package
  37. ** uses.  This all works only if you have System 7.5 or later (which incorporates
  38. ** a new Standard File Package introduced in System Update 3.0)
  39. **/
  40.  
  41. #include    <Files.h>
  42. #include    <Folders.h>
  43. #include    <Memory.h>
  44. #include    <GestaltEqu.h>
  45. #include    <ToolUtils.h>
  46. #include    <Resources.h>
  47.  
  48. #include    "GestaltValue.h"
  49.  
  50. enum {
  51.     gestaltStandardFileUseGenericIcons = 3,
  52.     kSFIconsPrefFileName = 128,
  53.     kOurCustomIcon = 128,
  54.     kOurGenericIcon = 129,
  55.     kFailedIcon = 130
  56. };
  57.  
  58.  
  59. /* From ShowIcon7.c */
  60. pascal void ShowIcon7(short iconId, Boolean advance);
  61.  
  62. void     main(void);
  63. Boolean UseGenericIcons(Handle);
  64. void    Generic(void);
  65. Boolean    OKSystem(void);
  66.  
  67. /*
  68. **    Get the name of our preference file from a 'STR ' resource
  69. **  in our control panel.  Check if that preference file exists.
  70. **  If that preference file exists, we should set the bit in the
  71. **  appropriate Gestalt selector which tells the standard file 
  72. **  package whether to use generic icons from within the PACK 
  73. **  (faster) or grab icons from the desktop file (slower).
  74. **/
  75. void main(void)
  76. {
  77.     Handle    prefFileName;
  78.  
  79.     if (!OKSystem())
  80.     {
  81.         ShowIcon7(kFailedIcon, true);
  82.         return;
  83.     }
  84.     prefFileName = (Handle)GetString(kSFIconsPrefFileName);
  85.     if (prefFileName != nil)
  86.     {
  87.         HLock(prefFileName);
  88.         if (UseGenericIcons(prefFileName))
  89.         {
  90.             Generic();
  91.             ShowIcon7(kOurGenericIcon, true);
  92.         }
  93.         else
  94.             ShowIcon7(kOurCustomIcon, true);
  95.         HUnlock(prefFileName);
  96.         ReleaseResource(prefFileName);        // as noticed by Jon Pugh
  97.     }
  98. }
  99.  
  100.  
  101.  
  102. /*
  103. ** Decide to use generic icons or not. See if the preference file exists.  If it 
  104. ** exists, use generic icons.
  105. **/
  106. Boolean UseGenericIcons(Handle preferenceFileName)
  107. {
  108.     OSErr    err;
  109.     FSSpec    spec;
  110.     short    foundVRefNum;
  111.     long    foundDirID;
  112.     
  113.     err = FindFolder(kOnSystemDisk,kPreferencesFolderType,false,&foundVRefNum,&foundDirID);
  114.     if (!err)
  115.         err = FSMakeFSSpec(foundVRefNum,foundDirID,(StringPtr)*preferenceFileName,&spec);
  116.     return (err == noErr);
  117. }
  118.  
  119. /*
  120. ** Generic
  121. ** 
  122. ** Set the Gestalt bit in gestaltStandardFileAttr which says to use Generic Icons
  123. **/
  124. void Generic(void)
  125. {
  126.     long    gestaltValue;
  127.     OSErr    err;
  128.     
  129.     err = Gestalt(gestaltStandardFileAttr, &gestaltValue);
  130.     if (!err)
  131.     {
  132.         gestaltValue |= (1 << gestaltStandardFileUseGenericIcons);
  133.         ReplaceGestaltValue(gestaltStandardFileAttr, gestaltValue);
  134.     }
  135. }
  136.  
  137.  
  138. /* Check System Version and return false if primary digit is < 7 (e.g. System 6)*/
  139. Boolean OKSystem(void)
  140. {
  141.     long    response;
  142.     OSErr    err;
  143.     
  144.     err = Gestalt(gestaltSystemVersion, &response);
  145.     
  146.     return ( (err == noErr) && ((response >> 8) >= 0x07) );
  147. }
  148.     
  149.